/* ==============================================================================
 * 创 建 者：lyt
 * 创建日期：2017-07-14
 * 功能描述：新增收货地址

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/

//获取地址详情
var getAddrUrl = baseUrlMall + 'MallF/GetMallAddr';

//编辑保存收货地址
var redactAddrUrl = baseUrlMall + 'MallF/SaveMallAddr';
//获取省市区
var getRegionUrl = baseUrl + 'common/GetRegion';

var vu;
var oid = getURLParameter('oid') || '';
var type = getURLParameter("type");// 1 提交订单页面选择收货地址  

$(function() {
	initVue();
	
});

function initVue(){
	vu = new Vue({
		el: '#content',
		data: {
			areaMsg: '', //省市区信息
			accepterMsg: {
				Province: '',
				City: '',
				County: '',
				Street: '',
				addrInfo: '',
				Isdefault: '',
				AccepterName: '',
				AccepterTel: ''
			},
			proCode : '',
			cityCode : '',
			areaCode : '',
			proList : [],
			cityList : [],
			areaList : [],
		},
		mounted: function() {
			var _this = this;
			this.getPro();
			if(oid){
				_this.getAdrMsg();
			}
		},
		methods: {
			getPro : getPro,
			getCity : getCity,
			getArea : getArea,
			getAdrMsg: getAdrMsg,
			adrInfo: function() {
				this.accepterMsg.addrInfo = this.areaMsg + ' ' + this.accepterMsg.Street
			},
			addNewAddress: addNewAddress
		}
	})
}

function getPro(){
	common.post(getRegionUrl, {}, 'json', function(r) {//code: oid
		vu.proList = r;
	})
}

function getCity(oid,cityID){
	if(!vu.proCode){
		return;
	}
	common.post(getRegionUrl, {code: vu.proCode}, 'json', function(r) {
		if(oid){//有oid时 为编辑地址获取信息
			vu.cityList = r;
			vu.cityCode = cityID;
		}else{//手动点击选择地址
			vu.cityList = r;
			vu.cityCode = '';
			vu.areaList = '';
			vu.areaCode = '';
		}
	})
}

function getArea(oid,areaID){
	if(!vu.cityCode){
		return;
	}
	common.post(getRegionUrl, {code: vu.cityCode}, 'json', function(r) {//code: oid
		if(oid){//有oid时 为编辑地址获取信息
			vu.areaList = r;
			vu.areaCode = areaID;
		}else{//手动点击选择地址
			vu.areaList = r;
			vu.areaCode = '';
		}
	})
}

function addNewAddress(){
	if(!vu.accepterMsg.AccepterName){
		mui.toast("请输入收货人姓名");
		return;
	};
	if(!vu.accepterMsg.AccepterTel){
		mui.toast("请输入收货人电话");
		return;
	};
	if(!vu.proCode){
		mui.toast("请选择省！");
		return;
	}
	if(!vu.cityCode){
		mui.toast("请选择市！");
		return;
	}
	if(!vu.areaCode){
		mui.toast("请选择区！");
		return;
	}
	if(!vu.accepterMsg.Street){
		mui.toast("请输入街道！");
		return;
	}
	vu.accepterMsg.Province = vu.proCode;
	vu.accepterMsg.City = vu.cityCode;
	vu.accepterMsg.County = vu.areaCode;
	vu.accepterMsg.addrInfo = vu.accepterMsg.Street;
	if($("#Isdefault").prop("checked")) {
		vu.accepterMsg.Isdefault = true;
	} else {
		vu.accepterMsg.Isdefault = false;
	}
	if(oid) {
		vu.accepterMsg.OID = oid;
		common.post(redactAddrUrl, vu.accepterMsg, 'json', function(r) {
			if(r.ErrorCode == 0) {
				var btnArray = ['确定'];
				mui.confirm('保存地址成功，点击确定跳转至地址列表！', '', btnArray, function(e) {
					if(type){
						location.href = 'addrList.html?type=' + type;
					}else{
						location.href = 'addrList.html';
					}
				})
			} else {
				mui.toast(r.ErrorMsg)
			}

		})
	} else {
		common.post(redactAddrUrl, vu.accepterMsg, 'json', function(r) {
			if(r.ErrorCode == 0) {
				var btnArray = ['确定'];
				mui.confirm('新增地址成功，点击确定跳转至地址列表！', '', btnArray, function(e) {
					if(type){
						location.href = 'addrList.html?type=' + type;
					}else{
						location.href = 'addrList.html';
					}
				})
			} else {
				mui.toast(r.ErrorMsg)
			}

		})
	}
}

function getAdrMsg(){
	common.post(getAddrUrl, {OID: oid}, 'json', function(r) {
			if(r.ErrorCode == 0) {
				vu.accepterMsg.AccepterName = r.Data.Addr.AccepterName;
				vu.accepterMsg.AccepterTel = r.Data.Addr.AccepterTel;
				vu.accepterMsg.Isdefault = r.Data.Addr.IsDefault;
				vu.proCode = r.Data.Addr.Province;
				vu.cityCode = r.Data.Addr.City;
				vu.areaCode = r.Data.Addr.County;
				
				vu.accepterMsg.Street = r.Data.Addr.Street;
				vu.getCity(vu.proCode,vu.cityCode);
				vu.getArea(vu.cityCode,vu.areaCode);
				if(vu.accepterMsg.Isdefault) {
					$("#Isdefault").prop("checked", true);
				} else {
					$("#Isdefault").prop("checked", '')
				}
			} else {
				mui.toast(r.ErrorMsg)
			}
		})
}
